<?php

include_once '../Connection.php';
include_once 'Database.php';

class ModelCreator_Database_SGBD_MySQL {

    private $conn;
    public $_test;
    public $_allTables;
    public $_allColumns;
    public $_allPrimaryKeys;

    public function __construct($host, $port, $db, $user, $password, $arrTables = NULL) {
        $this->connect($host, $port, $db, $user, $password);
        if ($this->testConnection()) {
            $this->getAllTables($db, $arrTables);
            $this->getAllColumns($this->_allTables);
            $this->getPrimaryKeys($this->_allTables);
        }
    }

    private function connect($host, $port, $db, $user, $password) {
        $this->conn = mysql_connect($host . ':' . $port, $user, $password);
        mysql_select_db($db);
        return $this->conn;
    }

    private function testConnection() {
        if (!$this->conn) {
            $this->_test = false;
            return FALSE;
        } else {
            $this->_test = true;
            return TRUE;
        }
    }

    public function getAllTables($db, $arrTables) {
        $sql = "SELECT DISTINCT(TABLE_NAME) as \"OBJECT_NAME\"
                FROM INFORMATION_SCHEMA.COLUMNS
                WHERE TABLE_SCHEMA = '{$db}'";
        if (!isset($arrTables['allTables']) && $arrTables['allTables'] != 'ok' && !is_null($arrTables)) {
            unset($arrTables['allTables']);
            $sql .= " AND TABLE_NAME IN (";
            $end = end($arrTables);
            foreach ($arrTables as $value) {
                if ($value == $end) {
                    $sql .= "'{$value}')";
                } else {
                    $sql .= "'{$value}',";
                }
            }
        }

        $result = mysql_query($sql);

        if (!$result) {
            echo "An error occured.\n";
            exit;
        }
        $i = 0;
        while ($array = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $arrayResult[$i] = $array['OBJECT_NAME'];
            $i++;
        }
        $this->_allTables = $arrayResult;
    }

    public function getAllColumns($tables) {
        foreach ($tables as $key => $value) {

            $sql = "SELECT COLUMN_NAME, DATA_TYPE
                    FROM INFORMATION_SCHEMA.COLUMNS
                    WHERE TABLE_NAME = '{$value}'";

            $result = mysql_query($sql);
            if (!$result) {
                echo "An error occured.\n";
                exit;
            }
            $i = 0;
            while ($array = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $arrayResult[$value][$i]['COLUMN'] = $array['COLUMN_NAME'];
                $arrayResult[$value][$i]['DATATYPE'] = $array['DATA_TYPE'];
                $i++;
            }
        }

        $this->_allColumns = $arrayResult;
    }

    public function getPrimaryKeys($tables) {

        foreach ($tables as $key => $value) {

            $sql = "SELECT COLUMN_NAME
                    FROM INFORMATION_SCHEMA.COLUMNS
                    WHERE COLUMN_KEY = 'PRI' AND TABLE_NAME = '{$value}';";

            $result = mysql_query($sql);
            if (!$result) {
                echo "An error occured.\n";
                exit;
            }
            $i = 0;
            while ($array = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $arrayResult[$value][$i] = $array['COLUMN_NAME'];
                $i++;
            }
        }

        $this->_allPrimaryKeys = $arrayResult;
    }

}

